package com._21cn._front.product.page.label;

import com._21cn.cms.common.Constants;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.utils.StringUtil;
import com._21cn.product.entity.*;
import com._21cn.product.service.CateService;
import com._21cn.product.service.WatchCateService;
import com._21cn.product.service.WatchPropertyService;
import com._21cn.product.service.WatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/4.
 */
@Component
public class WatchLabel {

    @Autowired(required = false)
    private WatchService watchService;

    @Autowired(required = false)
    private CateService cateService;

    @Autowired(required = false)
    private WatchPropertyService watchPropertyService;

    @Autowired(required = false)
    private WatchCateService watchCateService;

    /**
     * 获取手表列表
     * @param brandId
     * @param sex
     * @param gtPrice
     * @param ltPrice
     * @param sortRank
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageList getWatchList(String brandId, String sex, String gtPrice, String ltPrice, String sortRank,
                                     String pageNo, String pageSize){
        return getWatchList(brandId, sex, gtPrice, ltPrice, null, sortRank, pageNo, pageSize);
    }

    public PageList getWatchList(String brandId, String sex, String gtPrice, String ltPrice, String keyword, String sortRank,
                                            String pageNo, String pageSize){
        Map<String, Object> params = new HashMap<String, Object>();

        if(StringUtil.isNumber(brandId) && Integer.valueOf(brandId)>0){
            params.put("eqBrandId",brandId);
        }

        if(StringUtil.isNumber(sex) && Integer.valueOf(sex)>0){
            params.put("eqSex",sex);
        }

        if(StringUtil.isNumber(gtPrice) && Integer.valueOf(gtPrice)>0){
            params.put("gtSecondPrice",gtPrice);
        }

        if(StringUtil.isNumber(ltPrice) && Integer.valueOf(ltPrice)>0){
            params.put("ltSecondPrice",ltPrice);
        }

        if(!StringUtil.isEmpty(keyword)){
            params.put("likeName", keyword);
        }

        if(("priceDesc").equals(sortRank)){         //价格排序
            params.put("orderByClause", "SECOND_PRICE DESC");
        }else if(("priceAsc").equals(sortRank)){
            params.put("orderByClause", "SECOND_PRICE ASC");
        }

        if(("createTimeDesc").equals(sortRank)){    //创建排序
            params.put("orderByClause", "CREATE_TIME DESC");
        }else if(("createTimeAsc").equals(sortRank)){
            params.put("orderByClause", "CREATE_TIME ASC");
        }

        PageList<Watch> list = watchService.getList(params, Integer.valueOf(pageNo), Integer.valueOf(pageSize), true);
        for(Watch watch : list){
            //型号
            Map<String, Object> maps = new HashMap<String, Object>();
            maps.put("eqWatchId", watch.getId());
            maps.put("eqPropertyId", 14);   //型号
            List<WatchProperty> wpList = watchPropertyService.getList(maps, 1, 1, false);
            if(!wpList.isEmpty()){
                WatchProperty watchProperty = wpList.get(0);
                watch.setModelName(watchProperty.getPropertyValue());
            }
            //品牌
            maps.clear();
            maps.put("eqWatchId", watch.getId());
            maps.put("eqTypeId", Constants.CATE_TYPE_WATCH_BRAND);
            List<WatchCate> cateList = watchCateService.getList(maps, 1, 1);
            if(!cateList.isEmpty()){
                WatchCate watchCate = cateList.get(0);
                Integer cateId = watchCate.getCateId();
                Cate cate = cateService.getById(cateId);
                watch.setBrandName(cate.getName());
            }
        }
        return list;
    }

    /**
     * 获取推荐品牌
     * @return
     */
    public List getBrandList4Show(){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqTypeId", Constants.CATE_TYPE_WATCH_BRAND);
        params.put("eqStatus", Constants.STATUS_ENABLE);
        params.put("orderByClause", "SORT_NUM ASC");
        List<Cate> brandList = cateService.getList(params, 1, Constants.DEFAULT_PAGE_SIZE_MIDDLE);
        return brandList;
    }

    public Watch getWatch(String idStr){
        Integer id = Integer.valueOf(idStr);
        Watch watch = watchService.getById(id);
        return watch;
    }

}
